package org.itboys.mobile.controller.mobile;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.service.mongo.mobile.SundayMobileCodeService;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：jiangxiong
 * 日期：2017年2月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_验证码_controller。
 */
@Controller
@RequestMapping("/sunday/mobile/code")
public class SundayMobileCodeController extends BaseController {
    @Autowired
    private SundayMobileCodeService codeService;

    /**
     * 验证码发送
     * @param request
     * @param response
     * @param mobile
     * @param type
     */
    @RequestMapping(value = "/send2", method = RequestMethod.POST)
    @ApiOperation(value = "发送验证码。code!=0直接提示消息")
    public void send2(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "mobile", value = "手机号") @RequestParam(value = "mobile", required = true) String mobile,
            @ApiParam(required = true, name = "type", value = "验证码类型。1登陆（手机号加验证码登录），2注册" +
                    "，3修改密码，4忘记密码，5设置支付密码，6修改支付密码") @RequestParam(value = "type", required = true) Integer type
            ){
        try{

            int resultCode= codeService.sendCode(mobile,type);
            if(resultCode==-2){
                ResponseMessage.error(-2,"请输入正确的手机号。");
            }else{
                ResponseMessage.success();
            }
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code= CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code= CommonConstants.TOKEN_CODE;
                msg= CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/code/send2----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }


}
